﻿using System.Collections.Generic;
using System.Windows;

namespace NETworkManager.Utilities;

public class VisualTreeHelper
{
    public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null)
            yield break;

        for (var i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = System.Windows.Media.VisualTreeHelper.GetChild(depObj, i);

            if (child is T variable) yield return variable;

            foreach (var childOfChild in FindVisualChildren<T>(child)) yield return childOfChild;
        }
    }
}